package cn.edu.hpu.jie.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.edu.hpu.jie.util.DateUtil;
import cn.edu.hpu.jie.util.MimeMap;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private File imgFile;
	private String imgFileContentType;
	
	private String url;
	private int error;
	private String type;
	
	public String uploadFile() throws IOException {
		if(!"POST".equals(ServletActionContext.getRequest().getMethod())) {
			return ERROR;
		}
		
		if(imgFile != null) {
			String suffix = MimeMap.getType(imgFileContentType);
			
			if(suffix == null) {
				sendMsg("message", 1, "请上传图片");
				imgFile.delete();
				return null;
			}
			
			StringBuilder sb = new StringBuilder(ServletActionContext.getServletContext().getRealPath("/"));
			sb.append("image/");
			String date = DateUtil.getDate();
			sb.append(date);
			String savePath = sb.toString();
			
			File dirFile = new File(savePath);
			boolean result = true;
			if(!dirFile.isDirectory()) {
				result = dirFile.mkdirs();
			}
			
			if(result) {
				sb.append("/");
				String time = DateUtil.getTime();
				sb.append(time);
				sb.append("_");
				int random = new Random().nextInt(1000);
				sb.append(random);
				sb.append(".");
				sb.append(suffix);
				
				InputStream is = new FileInputStream(imgFile);
				OutputStream os = new FileOutputStream(new File(sb.toString()));
				
				byte[] buffer = new byte[1024];
				
				int len = -1;
				while((len = is.read(buffer)) != -1) {
					os.write(buffer, 0, len);
				}
				
				os.flush();
				is.close();
				os.close();
				
				String urlPath = sb.substring(sb.indexOf("image"));
				error = 0;
				type = "url";
				url = "/vote/" + urlPath;
			} 
		} else {
			error = 1;
			url = "服务器忙，请稍后再试...";
			type = "message";
		}
		
		
		
		imgFile.delete();
		
		sendMsg(type, error, url);
		
		return null;
	}

	private void sendMsg(String type, int error, String msg) throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		JsonObject jo = new JsonObject();
		jo.addProperty("error", error);
		jo.addProperty(type, msg);
		System.out.println(jo.toString());
		out.println(jo.toString());
		out.flush();
		out.close();
	}
	
	//get && set
	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}
}
